package com.suxii.test;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.suxii.Thread.NettyThread;
import com.suxii.adapter.ChatListAdapter;
import com.suxii.adapter.MessageAdapter;
import com.suxii.netty_demo.R;
import com.suxii.pojo.ChatMsg;
import com.suxii.pojo.MessageMsg;
import com.suxii.pojo.MyMsg;
import com.suxii.utils.Global;
import com.suxii.widget.NoBar;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main extends AppCompatActivity {
    private RecyclerView messageList;
    public static MessageAdapter messageAdapter;



    public static RecyclerView chat_list=null;
    public List<MyMsg> data;
    public static ChatListAdapter chatListAdapter;

    private EditText chat_in;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        //NoBar.setNoBar(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_main);

        messageList = findViewById(R.id.test_list);

        List<MessageMsg> list = new ArrayList<>();


        LinearLayoutManager manager = new LinearLayoutManager(this);
        messageList.setLayoutManager(manager);

        messageAdapter = new MessageAdapter(list, this);
        messageAdapter.findMessage();
        messageList.setAdapter(messageAdapter);



        init();

    }
    private void init(){
        View view=findViewById(R.id.test_in);
        chat_list=view.findViewById(R.id.chat_list);
        chat_in=view.findViewById(R.id.chat_in);
        title=view.findViewById(R.id.textView);
        title.setText(Global.uuid+"");
        Log.e("aa",Global.uuid+"");

        new Thread(()-> {
            data = new ArrayList<>();
            /*for (int i = 0; i < 3; i++) {
                data.add(new MyMsg(0, "msg" + i));
            }*/
            // 展示消息
            LinearLayoutManager manager = new LinearLayoutManager(this);
            chat_list.setLayoutManager(manager);
            chatListAdapter = new ChatListAdapter(data, this);
            //加载本地缓存消息
            chatListAdapter.findMessage2(Global.uuid);
            //chatListAdapter.findMessage(Global.uuid);
            chat_list.setAdapter(chatListAdapter);
            chat_list.scrollToPosition(chatListAdapter.getItemCount() - 1);

        },"线程更新消息").start();

        // 输入框焦点事件
        /*chat_in.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                // 此处为得到焦点时的处理内容
                Log.e("aa","获取焦点");
                chat_list.scrollToPosition(chatListAdapter.getItemCount()-1);
            } else {
                // 此处为失去焦点时的处理内容
                Log.e("aa","失去焦点");
            }
            chat_list.scrollToPosition(chatListAdapter.getItemCount()-1);
        });*/
        chat_in.setOnClickListener(v-> {
            chat_list.scrollToPosition(chatListAdapter.getItemCount() - 1);
        });

        title.setOnClickListener(v->{
            deleteMsg();
            chatListAdapter.deleteData();
        });
    }

    private boolean deleteMsg(){
        File file = new File(getFilesDir(), Global.uuid + "_message.sx");
        if (file.delete()) {
            return true;
        }
        return false;
    }

    public void msg_send(View view) {
        String text =chat_in.getText().toString();
        //发消息
        NettyThread.channel.writeAndFlush(new ChatMsg("",Global.uid,text,Global.uuid));
        chatListAdapter.addData(new MyMsg(1,text));
        chat_list.scrollToPosition(chatListAdapter.getItemCount()-1);

        chat_in.setText("");

        //chatListAdapter.savaMessage(Global.uuid);
        // 保存聊天消息
        chatListAdapter.savaMessage2(Global.uuid);

    }

}


